const { model } = require("../../models")
const { Op } = require("sequelize");
const uuid = require("uuid")
async function addUserOne({ username, password, createTime }) {
    var data = await model.user.create({
        id: uuid.v4(),
        username, password, createTime, active: 1
    })
    return JSON.parse(JSON.stringify(data))
}
async function getUser(username) {
    var data = await model.user.findOne({
        where: {
            username: username
        }
    });
    return JSON.parse(JSON.stringify(data))
}
async function updateUserTime({ oldTime, id }) {
    var data = await model.user.update({
        oldTime: oldTime,
    }, {
        where: {
            id: id
        }
    });
    return JSON.parse(JSON.stringify(data))
}
async function getAlluser({ username = "", pageSize = 10, pageNum = 1 }) {
    var data = await model.user.findAndCountAll({
        where: {
            username: {
                [Op.like]: "%" + username + "%"
            },
            active: 1,
        },
        attributes: ['id', "username", "createTime", "oldTime"],
        limit: pageSize,
        offset: (pageNum - 1) * pageSize,
        order: [
            ['createTime', 'DESC'],
        ]
    })
    return JSON.parse(JSON.stringify(data))
}

module.exports = {
    getUser,
    getAlluser,
    updateUserTime,
    addUserOne
}